﻿namespace BizVal.Contracts
{
    using System;
    using System.Collections.Generic;

    public class Hierarchy
    {
        private Dictionary<int, LabelSet> hierarchy;

        internal LabelSet this[int index]
        {
            get
            {
                return new LabelSet(1);
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Hierarchy"/> class.
        /// </summary>
        public Hierarchy()
        {
            this.hierarchy = new Dictionary<int, LabelSet>();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Hierarchy"/> class.
        /// </summary>
        /// <param name="numberOfLevels">The number of levels of the hierarchy.</param>
        /// <param name="minCardinality">Cardinality of the lower level of the hierarchy.</param>
        public Hierarchy(int numberOfLevels, int minCardinality)
        {
            this.hierarchy = new Dictionary<int, LabelSet>();
            this.InitializeHierarchy(numberOfLevels, minCardinality);
        }

        private void InitializeHierarchy(int numberOfLevels, int minCardinality)
        {
            int currentCardinality = minCardinality;

            for (int i = minCardinality; i < numberOfLevels; i++)
            {
                LabelSet level = new LabelSet(currentCardinality);
                this.hierarchy.Add(currentCardinality, level);
                currentCardinality = 2 * i - 1;
            }
        }

        /// <summary>
        /// Adds a level to the hierarchy with the specified cardinality.
        /// </summary>
        /// <param name="cardinality">The cardinality of the level to add.</param>
        public void Add(int cardinality)
        {
            LabelSet levelOfHierarchy = new LabelSet(cardinality);
            this.hierarchy.Add(cardinality, levelOfHierarchy);
        }
    }
}
